﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
using System.Data.SqlClient;

public partial class vote_VoteList : System.Web.UI.Page
{
    public string votes;
    //public string userid = "";
    public DataTable dt = new DataTable();
    public string more;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt.Columns.Add(new DataColumn("div"));
            dt.Columns.Add(new DataColumn("voters"));
            dt.Columns.Add(new DataColumn ("img"));
            dt.Columns.Add(new DataColumn ("name"));
        }
        if (Request["opration"] != null)
        {
            switch (Request["opration"].ToString())
            {
                case "created":
                    CreatedVoteBind(Request["num"].ToString());
                    break;
                case "joined":
                    JoinedVoteBind(Request["num"].ToString());
                    break;
                case "vote":
                    Vote();
                    break;
                case "hot":
                    VoteList("", "hot");
                    break;
                case "latest":
                    VoteList("", "latest");
                    break;
            }
        }
        

    }
    public void CreatedVoteBind(string num )
    {
        string sSql = " select " + num + " * from t_vote " +
                      " where inman='" + Session["userid"].ToString() + "' order by intime desc ";
        int count = TSqlDB.ExecuteScalar("select count(*) from t_vote where inman='" + Session["userid"].ToString() + "'");
        votes = "我发起了" + count.ToString() + "个投票";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        SqlDataReader drr = myReader.FDataReader;
        while (drr.Read())
        {
            string div = "<table>";
            div += "<tr><td><a href='voteshow.aspx?id="+drr["id"].ToString()+"'>" + drr["title"].ToString() + "</a></td></tr>";
            if(drr["description"].ToString()!="")
                div += "<tr><td>" + drr["description"].ToString() + "</td></tr>";

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and voteid not in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader1 = new TSqlDBReader(sSql);
            SqlDataReader drritem=myReader1.FDataReader;
            if (drritem.HasRows)
            {
                while (drritem.Read())
                {
                    if (drr["type"].ToString() == "1")
                        div += "<tr><td><input type='radio' name='" + drr["id"].ToString() + "' value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                    else
                        div += "<tr><td><input type='checkbox' name='" + drr["id"].ToString() + "'value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                }
                div += "<tr><td><input type='button' onclick=javascript:vote('" + drr["id"] + "') value='投票'></td></tr>";
            }
            drritem.Close();
            myReader1.Free();

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and id in ( select voteitemid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader2 = new TSqlDBReader(sSql);
            drritem = myReader2.FDataReader;
            if (drritem.HasRows)
            {
                
                while (drritem.Read())
                {
                    div += "<tr><td>" + drritem["content"].ToString() + "</td></tr>";
                }
            }
            div += "</table>";
            drritem.Close();
            myReader2.Free();

            DataRow dr = dt.NewRow();
            dr["div"] = div;
            dr["voters"]=drr["voters"];
            TUser ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));
            ouser.Select();
            if (ouser.MyPic != "")
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\" + ouser.MyPic + "' />";
            else
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\user.jpg' />";
            dr["name"] = ouser.BigName;

            dt.Rows.Add(dr);
           
        }
        drr.Close();
        myReader.Free();
        rpvote.DataSource = dt;
        rpvote.DataBind();
        if (count > 5 && num!="")
            more = "<a href='#' onclick='showmorecreated();'>查看更多 >></a>";
    }

    public void JoinedVoteBind(string num)
    {
        string sSql = " select " + num + " * from t_vote " +
                     " where id in (select voteid from t_vote_user where userid='" + Session["userid"].ToString() + "' )" + "order by intime desc ";
        int count = TSqlDB.ExecuteScalar("select count( distinct voteid) from t_vote_user where userid='" + Session["userid"].ToString() + "'");
        votes = "我参与了" + count.ToString() + "个投票";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        SqlDataReader drr = myReader.FDataReader;
        while (drr.Read())
        {
            string div = "<table>";
            div += "<tr><td><a href='voteshow.aspx?id=" + drr["id"].ToString() + "'>" + drr["title"].ToString() + "</a></td></tr>";
            if (drr["description"].ToString() != "")
                div += "<tr><td>" + drr["description"].ToString() + "</td></tr>";

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and voteid not in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader1 = new TSqlDBReader(sSql);
            SqlDataReader drritem = myReader1.FDataReader;
            if (drritem.HasRows)
            {
                while (drritem.Read())
                {
                    if (drr["type"].ToString() == "1")
                        div += "<tr><td><input type='radio' name='" + drr["id"].ToString() + "' value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                    else
                        div += "<tr><td><input type='checkbox' name='" + drr["id"].ToString() + "'value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                }
                div += "<tr><td><input type='button' onclick=javascript:vote('" + drr["id"] + "') value='投票'></td></tr>";
            }
            drritem.Close();
            myReader1.Free();

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and id in ( select voteitemid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader2 = new TSqlDBReader(sSql);
            drritem = myReader2.FDataReader;
            if (drritem.HasRows)
            {

                while (drritem.Read())
                {
                    div += "<tr><td>" + drritem["content"].ToString() + "</td></tr>";
                }
            }
            div += "</table>";
            drritem.Close();
            myReader2.Free();

            DataRow dr = dt.NewRow();
            dr["div"] = div;
            dr["voters"] = drr["voters"];
            TUser ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));
            ouser.Select();
            if (ouser.MyPic != "")
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\" + ouser.MyPic + "' />";
            else
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\user.jpg' />";
            dr["name"] = ouser.BigName;

            dt.Rows.Add(dr);
        }
        drr.Close();
        myReader.Free();
        rpvote.DataSource = dt;
        rpvote.DataBind();
        if (count > 5 && num != "")
            more = "<a href='#' onclick='showmorejoined()'>查看更多 >></a>";
    }
    public void Vote()
    {
        TVote oVote = new TVote(Request["vid"].ToString());
        oVote.Select();
        string[] items = Request["items"].ToString().Split(',');
        foreach (string str in items)
        {
            if (str != "")
            {
                TVoteUser oVoteUser = new TVoteUser();
                oVoteUser.Id = Guid.NewGuid().ToString();
                oVoteUser.UserId = Session["userid"].ToString();
                oVoteUser.VoteId = oVote.Id;
                oVoteUser.VoteItemId = str;
                oVoteUser.InMan = Session["userid"].ToString();
                oVoteUser.InTime = DateTime.Now;
                oVoteUser.Insert();
                TVoteItem oVoteItem = new TVoteItem(str);
                oVoteItem.Select();
                oVoteItem.Votes += 1;
                oVoteItem.Update();
            }
        }
        oVote.Voters += 1;
        oVote.Update();
    }

    public void VoteList(string num,string type)
    {
        string sSql ="";
        if(type=="hot")
            sSql = " select " + num + " * from t_vote " +
                      " order by voters desc ";
        else
            sSql = " select " + num + " * from t_vote " +
                      " order by intime desc ";
        
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        SqlDataReader drr = myReader.FDataReader;

        while (drr.Read())
        {
            string div = "<table>";
            div += "<tr><td><a href='voteshow.aspx?id=" + drr["id"].ToString() + "'>" + drr["title"].ToString() + "</a></td></tr>";
            if (drr["description"].ToString() != "")
                div += "<tr><td>" + drr["description"].ToString() + "</td></tr>";

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and voteid not in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader  myReader1 = new TSqlDBReader(sSql);
            SqlDataReader drritem = myReader1.FDataReader;
            if (drritem.HasRows)
            {
                while (drritem.Read())
                {
                    if (drr["type"].ToString() == "1")
                        div += "<tr><td><input type='radio' name='" + drr["id"].ToString() + "' value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                    else
                        div += "<tr><td><input type='checkbox' name='" + drr["id"].ToString() + "'value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                }
                div += "<tr><td><input type='button' onclick=javascript:vote('" + drr["id"] + "') value='投票'></td></tr>";
            }
            drritem.Close();
            myReader1.Free();

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and id in ( select voteitemid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader2 = new TSqlDBReader(sSql);
            drritem = myReader2.FDataReader;
            if (drritem.HasRows)
            {

                while (drritem.Read())
                {
                    div += "<tr><td>" + drritem["content"].ToString() + "</td></tr>";
                }
            }
            div += "</table>";
            drritem.Close();
            myReader2.Free();

            DataRow dr = dt.NewRow();
            dr["div"] = div;
            dr["voters"] = drr["voters"];
            TUser ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));
            ouser.Select();
            if (ouser.MyPic != "")
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\" + ouser.MyPic + "' />";
            else
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\user.jpg' />";
            dr["name"] = ouser.BigName;

            dt.Rows.Add(dr);
            ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));

        }
        drr.Close();

        myReader.Free();
        rpvote.DataSource = dt;
        rpvote.DataBind();
        
    }
    public string GetIdByEmail(string email)
    {
        string sSql = " select * from t_user where email='"+ email +"' ";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        if(myReader.FDataReader.HasRows )
            myReader.FDataReader.Read();
        sSql = myReader.FDataReader["uid"].ToString();
        myReader.Free();
        return sSql;

    }

    
}
